package com.shujia.flink.core

import java.time.Duration
import java.util.Properties

import org.apache.flink.streaming.api.functions.timestamps.BoundedOutOfOrdernessTimestampExtractor
import org.apache.flink.api.common.serialization.SimpleStringSchema
import org.apache.flink.streaming.api.TimeCharacteristic
import org.apache.flink.streaming.api.scala._
import org.apache.flink.streaming.api.windowing.time.Time
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer

object Demo5EventTime {
  def main(args: Array[String]): Unit = {

    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment


    //修改时间模式为事件时间
    env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime)


    val properties = new Properties()
    properties.setProperty("bootstrap.servers", "master:9092,node1:9092,node2:9092")
    properties.setProperty("group.id", "asdsadasd")
    /*

1,1622441880000
1,1622441881000
1,1622441882000
1,1622441883000
1,1622441883000
1,1622441884000
1,1622441885000
1,1622441883000
1,1622441886000
1,1622441887000
1,1622441888000
1,1622441890000
1,1622441891000
1,1622441892000
1,1622441895000
     */


    val consumer = new FlinkKafkaConsumer[String]("event_time", new SimpleStringSchema(), properties)


    val kafkaDS: DataStream[String] = env.addSource(consumer)


    val events: DataStream[Event] = kafkaDS.map(line => {
      val split: Array[String] = line.split(",")

      Event(split(0), split(1).toLong, 1)
    })


    //指定哪一个字段是事件事件
    //默认水位线等于最新一条数据的时间戳
    //val eventDS: DataStream[Event] = events.assignAscendingTimestamps(event => event.ts)


    //指定数据最大延迟时间和时间字段
    //相当于将水位线前移
    val eventDS: DataStream[Event] = events.assignTimestampsAndWatermarks(
      new BoundedOutOfOrdernessTimestampExtractor[Event](Time.seconds(5)) {
        //返回事件时间字段
        override def extractTimestamp(element: Event): Long = {
          element.ts
        }
      }
    )


    /**
      * 统计最近5秒每个id的数量
      *
      */

    val countDS: DataStream[Event] = eventDS
      .keyBy(_.id)
      .timeWindow(Time.seconds(5))
      .sum("c")


    countDS.print()

    env.execute()


  }


  case class Event(id: String, ts: Long, c: Long)

}
